package io.gitee.kotle.base.library

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.FrameLayout
import android.widget.LinearLayout
import androidx.appcompat.app.AppCompatActivity
import androidx.appcompat.widget.Toolbar
import androidx.core.content.ContextCompat
import io.gitee.kotle.android.mvvm.mvvminterface.IOnSingleClickListener
import io.gitee.kotle.android.mvvm.mvvminterface.MvvmViewType
import io.gitee.kotle.android.widget.BaseLinearLayout
import io.gitee.kotle.android.widget.BaseToolBar
import io.gitee.kotle.base.R

/**
 *
 * 描述：
 *
 * 创建人：jiale.wei
 * 创建时间：2021/11/23 11:17 上午
 *
 */
class UiViewHelperImpl : IUiViewHelper {
    private var uiView: BaseUiView? = null
    override val baseUiView: BaseUiView?
        get() = uiView
    private var bar: Toolbar? = null
    override val toolbar: Toolbar?
        get() = bar

    override fun createBaseUiView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?,
        needToolbar: Boolean,
        needBaseUi: Boolean
    ): ViewGroup? {
        return if (needToolbar) {
            BaseLinearLayout(inflater.context).apply {
                orientation = LinearLayout.VERTICAL
                layoutParams = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT,
                )
                addView(
                    createToolBar(inflater), LinearLayout.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.WRAP_CONTENT,
                    )
                )
                if (needBaseUi) {
                    addView(
                        createUiView(inflater), LinearLayout.LayoutParams(
                            LinearLayout.LayoutParams.MATCH_PARENT,
                            LinearLayout.LayoutParams.MATCH_PARENT,
                        )
                    )
                }
            }
        } else {
            if (needBaseUi) {
                createUiView(inflater).apply {
                    layoutParams = ViewGroup.LayoutParams(
                        LinearLayout.LayoutParams.MATCH_PARENT,
                        LinearLayout.LayoutParams.MATCH_PARENT,
                    )
                }
            } else {
                null
            }
        }
    }

    private fun createUiView(inflater: LayoutInflater): BaseUiView {
        return BaseUiView(inflater.context).apply {
            uiView = this
        }
    }

    private fun createToolBar(inflater: LayoutInflater): Toolbar {
        val ctx = inflater.context
        return BaseToolBar(ctx).apply {
            bar = this
            if (ctx is AppCompatActivity) {
                if (ctx.supportActionBar == null) {
                    ctx.setSupportActionBar(this)
                }
                setBackgroundResource(R.color.toolBarBackground)
                val drawable = ContextCompat.getDrawable(ctx, R.drawable.toolbarLogo)
                drawable?.setTint(ContextCompat.getColor(ctx, R.color.toolbarLogoTint))
                navigationIcon = drawable
                title = ctx.title
                popupTheme = R.style.toolbarPopupTheme
                setTitleTextAppearance(ctx, R.style.toolbarTitleTextAppearance)
                setNavigationOnClickListener(object : IOnSingleClickListener {
                    override fun onSingleClick(view: View) {
                        super.onSingleClick(view)
                        ctx.onBackPressed()
                    }
                })
            }
        }
    }

    override fun handleBaseUiView(
        inflater: LayoutInflater,
        beforeView: ViewGroup?,
        createView: MvvmViewType?,
        savedInstanceState: Bundle?
    ): MvvmViewType? {
        if (beforeView != null) {
            val child = when (createView) {
                is MvvmViewType.LayoutResType -> {
                    inflater.inflate(createView.layoutRes, beforeView, false)
                }
                is MvvmViewType.ViewBindingType -> {
                    createView.binding.root
                }
                is MvvmViewType.ViewInstanceType -> {
                    createView.view
                }
                else -> null
            }
            if (child != null && child.parent == null) {
                addChild(beforeView, child)
            }
            return MvvmViewType.ViewInstanceType(beforeView)
        }
        return createView
    }


    private fun addChild(beforeView: ViewGroup, child: View) {
        val uiView = baseUiView
        val lp = child.layoutParams ?: ViewGroup.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT
        )
        if (uiView != null) {
            uiView.addView(child, lp)
        } else {
            beforeView.addView(child, lp)
        }
    }
}